package com.eascs.crm.nsEditDetail.controller;

import java.text.Format;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.nsEditDetail.entity.AssetsTdInfoEntity;
import com.eascs.crm.nsEditDetail.entity.CarInfoEntity;
import com.eascs.crm.nsEditDetail.form.AssetsTdInfoForm;
import com.eascs.crm.nsEditDetail.form.CarInfoForm;
import com.eascs.crm.nsEditDetail.service.AssetsTdInfoService;
import com.eascs.crm.nsEditDetail.service.CarInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;

/**
 * 夫妻双方负债明细信息
 * 
 * @author lwx
 *
 */
@Controller
@RequestMapping("/td_assets")
public class AssetsTdInfoRestController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(AssetsTdInfoRestController.class);

	@Inject
	AssetsTdInfoService  assetsTdInfoService;

	@RequestMapping("/td_assets_list")
	public PaginationRestResult td_assets_list(String loanapplyId,@RequestParam(defaultValue="0") Integer showPage) {
		PageResult<Object[]> pageResult = assetsTdInfoService.findAssetsTdListInfo(loanapplyId,showPage);
		String[] oatitles = { "贷款类型", "贷款机构", "总额", "剩余本金", "担保方式", "起止日期","还款方式" };
		String o2ourl = "/td_assets/td_assets_list";
		if (StringUtil.isNotBlank(loanapplyId)) {
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanapplyId", loanapplyId);
			o2ourl = o2ourl + "&showPage=";
		} else {
			o2ourl = o2ourl + "?showPage=";
		}
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, oatitles, o2ourl);
		Button editButton = ButtonHelper.createAjaxButton("修改", "/td_assets/td_assets_op?assetsTdId=", "editUserMechanismInfo");
		Button delButton = ButtonHelper.createAjaxButton("  删除 ", "/td_assets/td_assets_del?assetsTdId=", "deleteUserMechanismInfo");
		Button[] detailButton = new Button[] { editButton, delButton };
		SelectDataOption[] selectDataOptions = {new SelectDataOption("DKLX"),new SelectDataOption("DKJG"), null,null,new SelectDataOption("DBFS"),null,new SelectDataOption("FKFS")};
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setRendTo("#o2o_td_assets");
		paginationInfo.setFormats(new Format[] { null, null, null, null, null, null,null });
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	// 新增修改
	@RequestMapping(value = "/td_assets_op")
	public HtmlRestResult tdAssetsOp(String assetsTdId,String loanapplyId) {
		AssetsTdInfoEntity vo = new AssetsTdInfoEntity();
		if (StringUtil.isNotBlank(assetsTdId)) {
			vo = assetsTdInfoService.findAssetsTdInfoEntity(assetsTdId);
		}
		ViewContext.put("vo", vo);
		ViewContext.put("loanapplyId", loanapplyId);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/td_assets/td_assets_op");
	}

	@RequestMapping(value = "/td_assets_del")
	public BaseRestResult<String> tdAssetsDel(String assetsTdId) {
		try {
			boolean flag = assetsTdInfoService.assetsTdDel(assetsTdId);
			if(flag){
				return BaseRestResult.info("操作成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_td_assets");
			}else{
				return BaseRestResult.info("操作失败").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_td_assets");
			}
		} catch (Exception e) {
			return BaseRestResult.warn("数据库操作异常");
		}
		
	}

	@RequestMapping("/td_assets_save")
	public BaseRestResult<String> tdAssetsSave(AssetsTdInfoForm form) {
		if (form != null && StringUtil.isNotBlank(form.getAssetsTdId())) {// 修改
			AssetsTdInfoEntity assetsTd = assetsTdInfoService.findAssetsTdInfoEntity(form.getAssetsTdId());
			try {
				boolean flag = assetsTdInfoService.assetsTdUpdate(assetsTd, form);
				if(flag){
					return BaseRestResult.info("修改成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_td_assets");
				}else{
					return BaseRestResult.info("修改失败").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_td_assets");
				}
			} catch (Exception e) {
				return BaseRestResult.warn("数据库操作异常");
			}
		} else {// 新增
			AssetsTdInfoEntity assetsTd = new AssetsTdInfoEntity();
			try {
				boolean flag = assetsTdInfoService.assetsTdSave(assetsTd, form);
				if(flag){
					return BaseRestResult.info("新增成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_td_assets");
				}else{
					return BaseRestResult.info("新增失败").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_td_assets");
				}
			} catch (Exception e) {
				return BaseRestResult.warn("数据库操作异常");
			}
		}
	}

}
